<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="artDialog/skins/default.css" type="text/css"></link>
<link href="css/select.css" rel="stylesheet" type="text/css" />
<title>DEMO</title>
<script type="text/javascript" src="js/apiv1.3.min.js"></script>
<!--script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script-->
<link rel="stylesheet" type="text/css" href="bmap.css" />
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/jquery.artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.plugins.min.js"></script>
<script type="text/javascript" src="js/util.js"></script>

</head>
<body>
	<div class="place">
		<span>位置：</span>
		<ul class="placeul">
			<li><a href="#">首页</a>
			</li>
			<li><a href="#">缺陷管理</a>
			</li>
			<li><a href="#">缺陷等级确认</a>
			</li>
		</ul>
		<span style="color: FF6633;">点击塔杆确认右击保存记录</span>
	</div>

	<div
		style="width:100%;height:95%;border:1px solid gray;position:absolute;"
		id="container"></div>
		

</body>
</html>
<script type="text/javascript">
	var mapOptions = {
		mapType : BMAP_NORMAL_MAP,
		maxZoom : 10, //地图最大层级
		minZoom : 5
	}
	var map = new BMap.Map("container", mapOptions); //设置卫星图为底图BMAP_PERSPECTIVE_MAP
	var initPoint = new BMap.Point(116.404, 39.915); // 创建点坐标

	map.centerAndZoom(initPoint, 5); // 初始化地图,设置中心点坐标和地图级别。
	map.enableScrollWheelZoom(); // 启用滚轮放大缩小。
	map.enableKeyboard(); // 启用键盘操作。  
	map.enableContinuousZoom(); //启用连续缩放效果

	// ----- control -----
	map.addControl(new BMap.NavigationControl()); //地图平移缩放控件
	map.addControl(new BMap.ScaleControl()); //显示比例尺在右下角

	// ----- Hide or Show Pole -----
	map.addEventListener("zoomend", function() {
		var overlays = map.getOverlays();
		for ( var i = 0; i < overlays.length - 1; i++) {
			if (overlays[i]._config.icon) {
				if (overlays[i]._config.icon.imageUrl.indexOf("images/circle")!=-1) {
					if (map.getViewport().zoom <= 5) {
						overlays[i].hide();
					} else {
						overlays[i].show();
					}
				}
			}
		}
	});
	



	function addPole(lng, lat, opts,color) {
		var point = new BMap.Point(lng, lat);
		var myIcon = new BMap.Icon("images/circle"+color+".png", new BMap.Size(16, 16));
		var t = opts;
			var overlays = map.getOverlays();
				for ( var i = 0; i < overlays.length ; i++) {
					if (overlays[i]._config) {
						var p = overlays[i]._config.point;
						
						if(p&&p.lng==lng&&p.lat==lat){
							/*
							t=overlays[i]._config.title+"、"+t;
							break;*/
							return ;
						}
					}
				}
	
		var marker = new BMap.Marker(point, {
			icon : myIcon,
			enableDragging : false,
			draggingCursor : "move",
			title : t
		});
		var contextMenu = new BMap.ContextMenu();
		var assignMenu = new BMap.MenuItem("保存记录",function(e,p,type){
				var taskNo=type._config.title.split(":")[0];
				
				
				art.dialog({title:"保存记录",content:"是否保存任务:"+taskNo+"的记录",lock:true,fixed:true,okValue:"保存",cancelValue:"取消",ok:function(){
						$.post("patrolTask!sure.action",{pid:taskNo},function(data){
							var json =eval("("+data+")");
							if(json.code==1){
								defaultAlert("保存记录","保存成功");
							}else{
								defaultAlert("保存记录","保存失败");
							}
							location.href="sureLevel.html";
						});
					return true;
				},cancel:function(){}});			
				},{width:50});
		contextMenu.addItem(assignMenu);
		marker.addContextMenu(contextMenu);
	
		 
		marker.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ t + "</span>");
		});
		marker.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
		});
		marker.addEventListener("click", function(type, target, point,
				pixel) {
				var arr = t.split("、");
				if(arr.length>1){
					var ids="";
					for(var i = 0;i<arr.length;i++){
						var tmp =arr[i].split(":")
						if(ids!=""){
							ids+=",";
						}
						ids+=tmp[0];
					}
					$.post("patrolTask!findByTaskNo.action",{pid:ids},function(data){
							var taskSelect = "<select class=\"taskSelect\" id='taskSelect'>";
							var task = eval("("+data+")");
							for(var i = 0;i<task.length;i++){
								taskSelect+="<option value='"+task[i].id+"'>"+task[i].name+"</option>"
							}
								taskSelect+="</select>";
							art.dialog({title:"选择任务",content:taskSelect,lock:true,fixed:true,okValue:"选择",cancelValue:"取消",ok:function(){
									$("#taskSelect").children().each(function(){
										if($(this).attr("selected")){
											sure_task($(this).val(),t);
										}
									});
								}
							});
							$(".taskSelect").uedSelect({width:180});
						
					});
				}else{
					var tmpdata =  arr[0].split(":")
					sure_task(tmpdata[0],tmpdata[1]);
				}
				
				});
		
		
		map.addOverlay(marker);
		return marker;
	}
	function sure_task(taskNo,poleNo){
		var pid=poleNo;
		if(pid.indexOf(":")!=-1){
			var arr = pid.split("、");
			for(var i=0;i<arr.length;i++){
				var tmp=arr[i].split(":");
				if(tmp[0]==taskNo){
					pid=tmp[1];
					break;
				}
			}
		}
		
		$.post("receiptPatrol!findByPidAndPtid.action",{"pid":pid,ptid:taskNo},function(r){
							var task = eval("("+r+")");
							var df_bid=-1;
							var df_lid=-1;
							var df_desc="";
							var df_rate="";
							if(task!=null){
								df_bid=task.bid;
								df_lid=task.level;
								df_desc=task.description;
								df_rate=task.intact;
							}
			$.post("bugType!findList.action",function(data){
				var bts = eval("("+data+")");
				var btSelect = "";
				for(var i = 0;i<bts.length;i++){
					btSelect+=(bts[i].id==df_bid ? bts[i].name:"");
				}
				var levelSelect ="<div class='vocation'><select class='levelSelect'>";
				levelSelect+="<option value='1' "+(df_lid==1?"selected='selected'":"")+">一般</option>";
				levelSelect+="<option value='2' "+(df_lid==2?"selected='selected'":"")+">严重</option>";
				levelSelect+="<option value='3' "+(df_lid==3?"selected='selected'":"")+">紧急</option>";
				levelSelect+="</select><div>";
				var html="<ul class='forminfo'>";			
				html+="<li><label>任务编号<b>*</b></label><label>"+taskNo+"</label></li>";
				html+="<li><label>杆塔编号<b>*</b></label><label>"+pid+"</label></li>";
				html+="<li><label>缺陷类型<b>*</b></label><label>"+btSelect+"</label>";
				html+="<li><label>完好率<b>*</b></label><label>"+df_rate+"</label></li>";
				html+="<li><label>缺陷描述<b>*</b></label><label>"+df_desc+"</label></li>";
				html+="<li><label>缺陷等级<b>*</b></label>"+levelSelect+"</li>";
				html+="</ul>";
				art.dialog({title:"缺陷等级确认",content:html,okValue:"确定",cancelValue:"取消",cancel:function(){},ok:function(){
					var levelId = $(".levelSelect").val();
				
					$.post("receiptPatrol!sure.action",{
						"bid":levelId,
						"pid":pid,
						"ptid":taskNo			
					},function(data){
						var json =eval("("+data+")");
						if(json.code==1){
							defaultAlert("缺陷等级确认","修改成功");
						}else{
							defaultAlert("缺陷等级确认","修改失败");
						}
						location.href="sureLevel.html";
					
					});
			//
				}});
			$(".levelSelect").uedSelect({width:130});
			
			});
		
		});
	
		
	}
	
	
	var routeDiv;
	function addRoute(points, opts, color) {

		var polyline = new BMap.Polyline(points, {
			strokeColor : color,
			strokeWeight : 3,
			strokeOpacity : 0.8,
			enableClicking :true
		});
		
		polyline.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ opts + "</span>");
					
		});
		polyline.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
			
		});
		
		
		map.addOverlay(polyline);
	}

	function calc_length(html) {
		var arr = html.split("<br>");
		var spoint = getPointByPointTitle($("#startNO").html());//获取起点point
		var epoint = getPointByPointTitle($("#endNO").html());//获取终点point
		var length = 0;
		if(arr.length>1){
			length=map.getDistance(spoint,getPointByPointTitle(arr[0]));
			
			for(var m = 1;m<arr.length;m++){
				length+=map.getDistance(getPointByPointTitle(arr[m-1]),getPointByPointTitle(arr[m]));
			}
			
			length +=map.getDistance(getPointByPointTitle(arr[arr.length-1]),epoint);
			
		}else{
			length=map.getDistance(spoint,epoint);
		}
		length =(length/1000)+"";
		var tmp = length.split(".");
		length = tmp[0]+"."+tmp[1].substring(0,3)+"公里";
		
		return length;	
	}
	function getPointByPointTitle(title){
		var overlays = map.getOverlays();
			for ( var i = 0; i < overlays.length ; i++) {
			if (overlays[i]._config) {
				if (overlays[i]._config.title==title) {
					return overlays[i]._config.point;
				}
			}
		}
	}
	
//findAllPolies
	$.post("patrolTask!findFinish.action",function(data){
			var json = eval("("+data+")");
			for(var i=0;i<json.length;i++){
				var polies = json[i];
				var points = new Array();
				var name = "";
				var points1 = new Array();
				var colors =["","","_yellow","_red"];
				for ( var x = 0; x < json[i].polies.length; x++) {
					points[x] = new BMap.Point(json[i].polies[x].lng,json[i].polies[x].lat);
					var color= colors[1];
					if(json[i].polies[x].receiptpatrol!=null){
						color = colors[json[i].polies[x].receiptpatrol.level];
					}
					if(json[i].polies[x].receiptpatrol){
						addPole(json[i].polies[x].lng, json[i].polies[x].lat, json[i].id+":"+json[i].polies[x].id,color);
					}
				}
				addRoute(points, json[i].name, "green");
  				
			}
	});
</script>
